package com.example.moonshine.redchange.Fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.moonshine.redchange.Activity.LaunchActivity;
import com.example.moonshine.redchange.Activity.PersonalActivity;
import com.example.moonshine.redchange.Activity.Setpsw;
import com.example.moonshine.redchange.Activity.banbenActivity;
import com.example.moonshine.redchange.Data.RSpostData;
import com.example.moonshine.redchange.Data.ResultData;
import com.example.moonshine.redchange.Data.ReturnPostData;
import com.example.moonshine.redchange.Util.CircleImageView;
import com.example.moonshine.redchange.Util.DataUrl;
import com.example.moonshine.redchange.Data.SidSelectData;
import com.example.moonshine.redchange.Helper.DataDBHepler;
import com.example.moonshine.redchange.R;
import com.example.moonshine.redchange.Util.SavePamasInfo;
import com.example.moonshine.redchange.Util.SaveParmasUtrl;
import com.example.moonshine.redchange.Util.SelecthttpUserUtil;
import com.google.gson.Gson;
import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.callback.Callback;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.content.ContentValues.TAG;

/**
 * Created by moonshine on 2018/3/10.
 */

public class SecondFragment extends Fragment {
    View            view;
    SavePamasInfo   mSavePamasInfo = new SavePamasInfo();
    CircleImageView mCircleImageView;
    TextView tv_userCode;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fg2, container, false);
        mCircleImageView = view.findViewById(R.id.h_head);
        TextView tv =(TextView) view.findViewById(R.id.change_paw);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(),Setpsw.class));
                getActivity().overridePendingTransition(R.anim.up_in, R.anim.up_out);

            }
        });
        TextView tv1=(TextView) view.findViewById(R.id.pensonal);
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(),PersonalActivity.class));
                getActivity().overridePendingTransition(R.anim.up_in, R.anim.up_out);

            }
        });
        TextView textView = view.findViewById(R.id.we);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(),banbenActivity.class));
                getActivity().overridePendingTransition(R.anim.up_in, R.anim.up_out);

            }
        });
        tv_userCode = view.findViewById(R.id.userCode);
        new Thread(new Runnable() {
            @Override
            public void run() {
                new AnotherTask().execute("");
            }
        }).start();

        final SelecthttpUserUtil selecthttpUserUtil = new SelecthttpUserUtil();
        Button btn_out = (Button)view.findViewById(R.id.login_out);
        btn_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Looper.prepare();
                        try {
                            SaveParmasUtrl saveParmasUtrl = new SaveParmasUtrl();
                            String SID = saveParmasUtrl.getInfo(getContext(),"sid","login");
                            selecthttpUserUtil.postSidhttp(SID,new DataUrl().secondFragLogout);
                            startActivity(new Intent(getActivity(), LaunchActivity.class));
                            getActivity().finish();
                        }catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                        Looper.loop();
                    }
                }).start();
            }

        });

        return view;
    }

    /**
     * @param keyCode
     * @param event   监听手机back键 点击返回界面
     * @return
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            //启动一个意图,回到桌面
            Intent backHome = new Intent(Intent.ACTION_MAIN);
            backHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            backHome.addCategory(Intent.CATEGORY_HOME);
            startActivity(backHome);
            return true;
        }
        return view.onKeyDown(keyCode, event);
    }
    @Override
    public void onResume() {
        super.onResume();
        Log.i("tag","在线");
        String path = null;
        try {
            path = mSavePamasInfo.getInfo(getContext(),"imgPath","file");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (path.equals("")){

            mCircleImageView.setImageResource(R.drawable.ic_header);
        }else {
            Bitmap bm = BitmapFactory.decodeFile(path);
            mCircleImageView.setImageBitmap(bm);
        }
    }


    @SuppressLint("StaticFieldLeak")
    private class AnotherTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPostExecute(String result) {
            //对UI组件的更新操作
            Gson gson = new Gson();
            try {
                RSpostData rSpostData= gson.fromJson(result, RSpostData.class);
                Log.i(TAG,"data数据为："+rSpostData.getData());

                ReturnPostData returnPostData = rSpostData.getData();
                tv_userCode.setText("工号："+returnPostData.getUserCode());

            }catch (Exception e){
                Toast.makeText(getContext(),"数据刷新错误",Toast.LENGTH_LONG).show();
                Log.e(TAG, "postlisthttp: ",e );
            }
        }
        @Override
        protected String doInBackground(String... params) {
            //耗时的操作
            String  SidStatus = null;
            String result = null;
            String url = "http://119.23.219.22:80/element-admin/user/query-self";
            OkHttpClient client = new OkHttpClient();
            Gson gson = new Gson();

            ResultData mdata = new ResultData();
            String sid =selectSid();

            mdata.setSid(sid);

            String json = gson.toJson(mdata);//将其转换为JSON数据格式

            MediaType mediaType = MediaType.parse("application/json; charset=utf-8");

            RequestBody requestBody = RequestBody.create(mediaType, json);//放进requestBoday中
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();
            try {
                Response response = client.newCall(request).execute();
                result = response.body().string();

            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, "doInBackground: ",e );
            }
            return result;
        }
    }

    public String  selectSid(){
//        DataDBHepler dataDBHepler = new DataDBHepler(getBaseContext());
//        ArrayList<SidSelectData> DataList = dataDBHepler.FindSidData();
//        final SidSelectData data = new SidSelectData(DataList.get(0).getId(),DataList.get(0).getSid(),DataList.get(0).getSysids());
//        Log.i(TAG,"数据库的sid为："+data.getSid());
        final String Msid =mSavePamasInfo.getInfo(getContext(),"sid","login") ;
        return Msid;
    }
}
